/**
 * @(#) GroupWisrPDFView.java
 * Copyright (c) 2011 EdRP, Dayalbagh Educational Institute.
 * All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * Redistributions of source code must retain the above copyright
 * notice, this  list of conditions and the following disclaimer.
 *
 * Redistribution in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in
 * the documentation and/or other materials provided with the
 * distribution.
 *
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL ETRG OR ITS CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Contributors: Members of EdRP, Dayalbagh Educational Institute
 */

package in.ac.dei.edrp.cms.controller.reportgeneration;



import in.ac.dei.edrp.cms.dao.reportgeneration.SubjectWiseMeritListDAO;
import in.ac.dei.edrp.cms.domain.reportgeneration.SubjectWiseMeritList;

import java.awt.Color;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.view.document.AbstractPdfView;


import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;

public class GroupWiseMeritListPDFView extends AbstractPdfView {
	

	private SubjectWiseMeritListDAO subjectWiseMeritListDao;	
		
	public void setSubjectWiseMeritListDAO(SubjectWiseMeritListDAO subjectWiseMeritListDao) {
		System.out.println("in set");
		this.subjectWiseMeritListDao = subjectWiseMeritListDao;
	}

	
	@SuppressWarnings("unchecked")
	protected void buildPdfDocument(Map model,Document document,PdfWriter writer,
			HttpServletRequest request,HttpServletResponse response) throws Exception
	{
		System.out.println("inside pdf view controller");
				
		List<SubjectWiseMeritList> studentDataList;
		
		studentDataList = (List<SubjectWiseMeritList>) model.get("StudentList");
		
		String programCode=studentDataList.get(0).getProgramCode();
		String branchId=studentDataList.get(0).getBranchId();
		String courseGroup=studentDataList.get(0).getCourseGroup();
		
		writer = PdfWriter.getInstance(document,new FileOutputStream("E:\\Major-Group-Wise Merit List_"+programCode+"_"+branchId+"_"+courseGroup+".pdf"));
		
		document.open();
		String headerText1 = "Major-Group-Wise Merit List";
		Paragraph header1 = new Paragraph(headerText1,FontFactory.getFont(FontFactory.HELVETICA,8, Font.NORMAL, new Color(0, 0, 255)));		
		header1.setAlignment(Element.ALIGN_CENTER);
		document.add(header1);
		
		Font cellFont = new Font(Font.HELVETICA, 8);
		
		Table studentTable = new Table(8,3);
		
		//	nameHeader.setBorderColor(new Color(255,255,255));
			
			Cell c1 = new Cell(new Phrase(" S. No. ",cellFont));		
			c1.setHeader(true);
			c1.setBorder(Rectangle.NO_BORDER);
			studentTable.addCell(c1);	
			
			c1 = new Cell(new Phrase(" CL ",cellFont));
//			c1.setHorizontalAlignment( Element.ALIGN_RIGHT);
			c1.setBorder(Rectangle.NO_BORDER);
			studentTable.addCell(c1);
			
			c1 = new Cell(new Phrase(" Br ",cellFont));
//			c1.setHorizontalAlignment( Element.ALIGN_RIGHT);
			c1.setBorder(Rectangle.NO_BORDER);
			studentTable.addCell(c1);
			
			c1 = new Cell(new Phrase(" Group ",cellFont));
//			c1.setColspan(3);
//			c1.setRowspan(2);
			c1.setBorder(Rectangle.NO_BORDER);
			studentTable.addCell(c1);		
			
			c1 = new Cell(new Phrase(" Roll No. ",cellFont));
			c1.setBorder(Rectangle.NO_BORDER);
			studentTable.addCell(c1);
			
			c1 = new Cell(new Phrase(" Name ",cellFont));
//			c1.setMaxLines(200);
//			c1.setRowspan(200);
//			c1.setColspan(200);
			c1.setWidth(80f);
//			c1.setWidth("800");
//			c1.setUseVariableBorders(true);
//			c1.setBorderWidth(60);
			
			c1.setBorder(Rectangle.NO_BORDER);
			studentTable.addCell(c1);
			
			c1= new Cell(new Phrase(" M/F ",cellFont));		
//			c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
			c1.setBorder(Rectangle.NO_BORDER);
			studentTable.addCell(c1);
			
			c1= new Cell(new Phrase(" Marks ",cellFont));		
//			c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
			c1.setBorder(Rectangle.NO_BORDER);
			studentTable.addCell(c1);
			
			PdfContentByte pcb = writer.getDirectContent();		
			pcb.moveTo(70,767f);
			pcb.lineTo(PageSize.A4.getWidth()-70,767f);
			pcb.setLineDash(3f,3f);
			pcb.stroke();
			
//			document.add( Chunk.NEWLINE ); to add a blank line in document
		    
			for(int i=0;i<studentDataList.size();i++){
				String ii = Integer.toString(i+1);
				c1 = new Cell(new Phrase(ii,cellFont));
				c1.setBorder(Rectangle.NO_BORDER);
				studentTable.addCell(c1);

				c1 = new Cell(new Phrase(studentDataList.get(i).getProgramCode(),cellFont));
				c1.setBorder(Rectangle.NO_BORDER);
				studentTable.addCell(c1);

				c1 = new Cell(new Phrase(studentDataList.get(i).getBranchId(),cellFont));
				c1.setBorder(Rectangle.NO_BORDER);
				studentTable.addCell(c1);

				c1 = new Cell(new Phrase(studentDataList.get(i).getCourseGroup(),cellFont));
				c1.setBorder(Rectangle.NO_BORDER);
				studentTable.addCell(c1);

				c1 = new Cell(new Phrase(studentDataList.get(i).getRollNumber(),cellFont));
				c1.setBorder(Rectangle.NO_BORDER);
				studentTable.addCell(c1);

				c1 = new Cell(new Phrase(studentDataList.get(i).getName(),cellFont));
				c1.setBorder(Rectangle.NO_BORDER);
				studentTable.addCell(c1);

				c1 = new Cell(new Phrase(studentDataList.get(i).getGender(),cellFont));
				c1.setBorder(Rectangle.NO_BORDER);
				studentTable.addCell(c1);

				c1 = new Cell(new Phrase(studentDataList.get(i).getMarks(),cellFont));
				c1.setBorder(Rectangle.NO_BORDER);
				studentTable.addCell(c1);
			}
			
			studentTable.setBorderWidth(0);		
			studentTable.setBorder(Rectangle.NO_BORDER);
			studentTable.endHeaders();		
			document.add(studentTable);
			document.close();		
	}
	
}
